package cn.neu.leon.servlet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.neu.leon.util.Sentiment;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Offline extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String csvName = "E://library/weibo/2011-10.csv";
		File file = new File(csvName);
		BufferedReader br = new BufferedReader(new FileReader(file));
		String str = null;
		JSONArray data = new JSONArray();
		double per[][] = new double[7][31];
		int line = 0;
		while((str=br.readLine())!=null){
			String[] strArray = str.split(",");
			for(int i=0;i<7;i++){
				per[i][line] = Double.parseDouble(strArray[i]);
			}
			line++;
		}
		for(int i=0;i<7;i++){
			JSONObject jo = new JSONObject();
			jo.put("name",Sentiment.sentimentCN[i]);
			jo.put("data", per[i]);
			data.add(jo);
		}
		
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter pw = response.getWriter();
		pw.print(data);
		pw.flush();
		pw.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
